package de.arndt_panzer.bensgallery;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Point;
import android.os.Bundle;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Message;
import android.util.Log;
import android.view.Display;
import android.view.KeyEvent;
import android.view.WindowManager;

public abstract class BensGalleryActivityBase extends Activity implements
		Callback {

	@Override
	public boolean handleMessage(Message msg) {
		// TODO Auto-generated method stub
		return false;
	}

	public static final String TAG = BensGalleryActivityBase.class
			.getSimpleName();

	Thread mBackTimeout;
	final Handler mStatusCallback = new Handler(this);

	@Override
	public void onBackPressed() {

	}

	private boolean pressedBackTimeout() {
		return (mBackTimeout != null && mBackTimeout.isAlive());
	}

	private void triggerPressedBackTimeout() {
		if (!pressedBackTimeout()) {
			mBackTimeout = new Thread(new Runnable() {

				@Override
				public void run() {
					int cnt = 50;
					try {
						while (cnt > 0) {
							Thread.sleep(100);
							--cnt;
						}
						;
					} catch (Exception e) {
						;
					}
				}
			});
			mBackTimeout.start();
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.isLongPress()) {
			if (pressedBackTimeout()) {
				startActivityForResult(new Intent(
						android.provider.Settings.ACTION_SETTINGS), 0);
				finish();
			} else
				triggerPressedBackTimeout();
		} else if (keyCode == KeyEvent.KEYCODE_VOLUME_UP
				|| keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
			return super.onKeyDown(keyCode, event);
		} else if (keyCode == KeyEvent.KEYCODE_CAMERA) {
			Intent intent = new Intent(this, SnappyCamActivity.class);
			intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			startActivity(intent);
		}

		return true;
	}

	protected int[] getWindowSize() {

		Display display = getWindowManager().getDefaultDisplay();
		Point size = new Point();
		display.getSize(size);
		return new int[] { size.x, size.y };
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		Log.d(TAG, "onCreate()");
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		Log.d(TAG, "onDestroy()");
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		Log.d(TAG, "onPause()");
	}

	@Override
	protected void onRestart() {
		// TODO Auto-generated method stub
		super.onRestart();
		Log.d(TAG, "onRestart()");
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		Log.d(TAG, "onResume()");

		StatusBroadcastService.addReceiver(mStatusCallback);
		startService(new Intent(this, StatusBroadcastService.class));
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		Log.d(TAG, "onStart()");
	}

	@Override
	protected void onStop() {
		super.onStop();
		Log.d(TAG, "onStop()");

		stopService(new Intent(this, StatusBroadcastService.class));
		StatusBroadcastService.removeReceiver(mStatusCallback);
	}

}
